package baiqitun.stupro.subject.core.task.impl;

import baiqitun.stupro.security.entity.SysUser;
import baiqitun.stupro.security.service.SysUserService;
import baiqitun.stupro.common.workflow.task.AbstractBooleanServiceTaskHandler;
import baiqitun.stupro.subject.entity.SysStudentInfo;
import baiqitun.stupro.subject.service.SysStudentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author kedaji
 */
@Service("cancelSelectedTutorServiceTaskHandler")
public class CancelSelectedTutorServiceTaskHandler extends AbstractBooleanServiceTaskHandler {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysStudentInfoService sysStudentInfoService;

    @Override
    public void handleTrue(DelegateExecution execution) {
        HistoricTaskInstance historicTaskInstance = getHistoricTaskInstance(execution);
        //查找当前学生的信息
        SysStudentInfo queryStudentInfo = new SysStudentInfo();
        queryStudentInfo.setUserId(historicTaskInstance.getOwner());
        SysStudentInfo resultStudentInfo = sysStudentInfoService.getOne(new QueryWrapper<>(queryStudentInfo));
        System.out.println(resultStudentInfo);
        // 使用学生信息的id 来修改将学生信息中的tutorId设置为空
        SysUser user = sysUserService.getById(resultStudentInfo.getUserId());
        sysStudentInfoService.update(new UpdateWrapper<SysStudentInfo>().eq("user_id", resultStudentInfo.getUserId()).set("tutor_id",null).set("update_by",user.getUsername()));
        //TODO 给学生发条消息
    }

    @Override
    public void handleFalse(DelegateExecution execution) {
        //TODO 给学生发条消息
    }
}
